function findOrder(numCourses: number, prerequisites: number[][]): number[] {
    const edges: number[][] = [];
    const visit: boolean[] = [];
    const ans: number[] = [];
    let avliable = true;

    for (let i = 0; i < numCourses; i++) {
        edges[i] = [];
    }

    for (const [x, y] of prerequisites) {
        edges[y].push(x);
    }

    const dfs = (val: number) => {
        if (!avliable) {
            ans.length = 0;
            return;
        }

        visit[val] = true;
        for (const v of edges[val]) {
            if (!visit[v]) {
                dfs(v);
            } else {
                avliable = false;
            }
        }

        if (ans.indexOf(val) === -1) {
            ans.push(val);
        }
        visit[val] = false;
    };

    for (let i = 0; i < numCourses; i++) {
        dfs(i);
    }

    ans.reverse();

    return ans;
};
